<?php

/**
 * This is the model class for table "turma_has_aluno".
 *
 * The followings are the available columns in table 'turma_has_aluno':
 * @property integer $id
 * @property integer $turma_int
 * @property integer $aluno_id
 * @property integer $responsavel_id
 * @property string $data_avaliacao1
 * @property string $data_avaliacao2
 *
 * The followings are the available model relations:
 * @property Avaliacao[] $avaliacaos
 * @property AvaliacaoDiaria[] $avaliacaoDiarias
 * @property Funcionario $responsavel
 * @property Aluno $aluno
 * @property Turma $turmaInt
 */
class TurmaHasAluno extends CActiveRecord
{
	public static $status = array(
		'Ativo' => 'Ativo',
		'Inativo' => 'Inativo',
		'Ausente' => 'Ausente',
	);
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'turma_has_aluno';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				array('turma_int, aluno_id', 'required'),
				array('turma_int, aluno_id, responsavel_id', 'numerical', 'integerOnly'=>true),
				array('data_avaliacao1, data_avaliacao2', 'safe'),
				// The following rule is used by search().
				// @todo Please remove those attributes that should not be searched.
				array('id, turma_int, aluno_id, responsavel_id, data_avaliacao1, data_avaliacao2', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'avaliacaos' => array(self::HAS_MANY, 'Avaliacao', 'turma_has_aluno_id'),
				'avaliacaoDiarias' => array(self::HAS_MANY, 'AvaliacaoDiaria', 'turma_has_aluno_id'),
				'frequencias' => array(self::HAS_MANY, 'Frequencia', 'turma_has_aluno_id'),
				'responsavel' => array(self::BELONGS_TO, 'Funcionario', 'responsavel_id'),
				'aluno' => array(self::BELONGS_TO, 'Aluno', 'aluno_id'),
				'turmaInt' => array(self::BELONGS_TO, 'Turma', 'turma_int'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'turma_int' => 'Turma',
				'aluno_id' => 'Aluno',
				'responsavel_id' => 'Responsavel',
				'data_avaliacao1' => 'Data da Primeira Avaliacao',
				'data_avaliacao2' => 'Data da Segunda Avaliacao',
				'status' => 'Estado',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->addCondition(array('turma_int'=>$this->turma_int));
		$criteria->compare('id',$this->id);
		$criteria->compare('turma_int',$this->turma_int);
		$criteria->compare('aluno_id',$this->aluno_id);
		$criteria->compare('responsavel_id',$this->responsavel_id);
		$criteria->compare('data_avaliacao1',$this->data_avaliacao1,true);
		$criteria->compare('data_avaliacao2',$this->data_avaliacao2,true);
		$criteria->compare('status', $this->status, true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	
	public function search_by_responsavel($id) {
		$criteria=new CDbCriteria;
		if($id > 0) {
			$criteria->addCondition('t.responsavel_id = :responsavel_id');
			$criteria->params = array(':responsavel_id' => $id);
		}
		$criteria->compare('id',$this->id);
		$criteria->compare('turma_int',$this->turma_int);
		$criteria->compare('aluno_id',$this->aluno_id);
		$criteria->compare('responsavel_id',$this->responsavel_id);
		$criteria->compare('data_avaliacao1',$this->data_avaliacao1,true);
		$criteria->compare('data_avaliacao2',$this->data_avaliacao2,true);
		$criteria->compare('status', $this->status, true);
		
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

	public function constructed_search() {
		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('turma_int',$this->turma_int);
		$criteria->compare('aluno_id',$this->aluno_id);
		$criteria->compare('responsavel_id',$this->responsavel_id);
		$criteria->compare('data_avaliacao1',$this->data_avaliacao1,true);
		$criteria->compare('data_avaliacao2',$this->data_avaliacao2,true);
		$criteria->compare('status', $this->status, true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}


	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return TurmaHasAluno the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function delete() {
		if($this->id != null && $this->id > 0) {
			AvaliacaoDiaria::model()->deleteAllByAttributes(array('turma_has_aluno_id'=>$this->id));		
			Frequencia::model()->deleteAllByAttributes(array('turma_has_aluno_id'=>$this->id));
			Avaliacao::model()->deleteAllByAttributes(array('turma_has_aluno_id'=>$this->id));
		}
		parent::delete();
	}
}
